//import org.activiti.bpmn.model.BpmnModel;
//import org.activiti.bpmn.model.FlowElementsContainer;
//import org.activiti.bpmn.model.FlowNode;
//import org.activiti.bpmn.model.SequenceFlow;
//import org.activiti.engine.HistoryService;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.RuntimeService;
//import org.activiti.engine.TaskService;
//import org.activiti.engine.history.HistoricActivityInstance;
//import org.activiti.engine.history.HistoricTaskInstance;
//import org.activiti.engine.impl.identity.Authentication;
//import org.activiti.engine.runtime.Execution;
//import org.activiti.engine.task.Task;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.UUID;
//
///**
// * 回到上一个任务
// */
//@Component
//public class ActivitiWithdrawUtils {
//    @Autowired
//    private com.lenovo.qms.activiti.service.util.MyActivitiUtilFor6x myActivitiUtilFor6x;
//
//    public void withdraw(String taskId ) {
//        TaskService taskService = myActivitiUtilFor6x.getTaskService();
//        HistoryService historyService = myActivitiUtilFor6x.getHistoryService();
//        RepositoryService repositoryService = myActivitiUtilFor6x.getRepositoryService();
//        RuntimeService runtimeService = myActivitiUtilFor6x.getRuntimeService();
//        Task task = myActivitiUtilFor6x.getTaskService().createTaskQuery().taskId(taskId).singleResult();
//        HistoricTaskInstance myTask = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
//        String processDefinitionId = myTask.getProcessDefinitionId();
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
//        String myActivityId = null;
//
//        List<HistoricActivityInstance> haiList =
//                historyService
//                        .createHistoricActivityInstanceQuery()
//                        .executionId(myTask.getExecutionId())
//                        .finished()
//                        .list();
//
////        for (HistoricActivityInstance hai : haiList) {
////            if(myTask.getId().equals(hai.getTaskId())) {
////                myActivityId = hai.getActivityId();
////                break;
////            }
////        }
//        if(haiList.isEmpty()){
//            System.out.println("没有任务");
//            return ;
//        }
//        int i = 1;
//        while(myActivityId == null){
//            HistoricActivityInstance hai =  haiList.get(haiList.size() -i);
//            i++ ;
//            // 判断网关
//            if(hai.getActivityId().startsWith("gtw")){
//                if(i == haiList.size()){
//                    break;
//                }
//                continue;
//            }
//            myActivityId = hai.getActivityId();
//        }
//
//        FlowNode myFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(myActivityId);
//        //考虑子流程
//        if(myFlowNode == null){
//            FlowElementsContainer parent  =  bpmnModel.getMainProcess().getFlowElementsContainer(myActivityId);
//            if(parent != null){
//                myFlowNode = (FlowNode) parent.getFlowElement(myActivityId);
//            }
//        }
//        if(myFlowNode == null){
//            throw new RuntimeException("没有找到节点");
//        }
//        Execution execution = runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult();
//        String activityId = execution.getActivityId();
//        System.out.println(activityId);
//
//        FlowNode flowNode = (FlowNode) bpmnModel.getMainProcess()
//                .getFlowElement(activityId);
//        if(flowNode == null){
//            FlowElementsContainer parent  =  bpmnModel.getMainProcess().getFlowElementsContainer(activityId);
//            if(parent != null){
//                flowNode = (FlowNode) parent.getFlowElement(activityId);
//            }
//        }
//        if(flowNode == null){
//            throw new RuntimeException("没有找到节点");
//        }
//        //记录原活动方向
//        List<SequenceFlow> oriSequenceFlows = new ArrayList<SequenceFlow>();
//        oriSequenceFlows.addAll(flowNode.getOutgoingFlows());
//        //清理活动方向
//        flowNode.getOutgoingFlows().clear();
//        //建立新方向
//        List<SequenceFlow> newSequenceFlowList = new ArrayList<SequenceFlow>();
//        SequenceFlow newSequenceFlow = new SequenceFlow();
//        newSequenceFlow.setId(UUID.randomUUID().toString());
//        newSequenceFlow.setSourceFlowElement(flowNode);
//        newSequenceFlow.setTargetFlowElement(myFlowNode);
//        newSequenceFlowList.add(newSequenceFlow);
//        flowNode.setOutgoingFlows(newSequenceFlowList);
//        taskService.addComment(task.getId(), task.getProcessInstanceId(), "撤回");
//        Map map =  myActivitiUtilFor6x.queryVarableByTaskId(taskId);
//        //完成任务
//        myActivitiUtilFor6x.completeTask(task.getId(),map);
////        taskService.complete(task.getId());
//        //恢复原方向
//        flowNode.setOutgoingFlows(oriSequenceFlows);
//    }
//
//
//    TaskService taskService = activitiUtil.getTaskService();
//    // 获取登陆用户信息
//    SysUserEntity userEntity = rbacService.getLoginUser();
//    IssDynProcessEntity query = new IssDynProcessEntity();
//        query.setJobno(jobno);
//    IssDynProcessEntity issDynProcessEntity = issDynProcessMapper.queryByCondition(query);
//        MyAssertUtils.isNotNull(owner, "TaskOwnerList can not be null");
//        MyAssertUtils.isNotNull(issDynProcessEntity, "jobno can not be found");
//    // 指派多个taskOwner
//    List<String> oldTaskOwnerList = new ArrayList<>();
//    // 根据 job no 查询所有的任务
//    List<Task> allTasks = activitiUtil.queryTaskByBusinessNo(jobno) ;
//    List<Task> tasks = Lists.newArrayList();
//    // 过滤需要重新指派的任务
//        for(Task task : allTasks){
//        if(taskIds.indexOf(task.getId()) != -1){
//            tasks.add(task);
//        }
//    }
//        for (Task task : tasks) {
//        String taskId = task.getId();
//        List<IdentityLink> candidateList = taskService.getIdentityLinksForTask(taskId);
//        for (IdentityLink identityLink : candidateList) {
//            oldTaskOwnerList.add(identityLink.getUserId());
//            taskService.deleteUserIdentityLink(taskId, identityLink.getUserId(), "candidate");
//        }
//        // 添加新的候选人、任务负责人
//        taskService.addUserIdentityLink(taskId, owner, "candidate");
//        taskService.setAssignee(taskId, owner);
//    }
//    IssNodeRecordEntity queryIssNode = new IssNodeRecordEntity();
//        queryIssNode.setJobno(jobno);
//    List<IssNodeRecordEntity> nodes = issNodeRecordMapper.findList(queryIssNode);
//    // 这里数据量不会大，就一条一天循环插入 避免代码有其他逻辑问题
//        for (Task task : tasks) {
//        // 增加节点日志记录
//        IssNodeRecordEntity issNodeRecord = new IssNodeRecordEntity();
//        issNodeRecord.setJobno(jobno);
//        String currentUser = userEntity.getUsername();
//        issNodeRecord.setApproveUserId(currentUser);
//        issNodeRecord.setIssueCode(issDynProcessEntity.getIssueCode());
//        issNodeRecord.setOperation(ProcessIqcEnum.REASSIGN.getCode());
//        issNodeRecord.setTaskId(task.getId());
//
//        issNodeRecord.setProductLine(issDynProcessEntity.getProductLine());
//        issNodeRecord.setExecutorUserId(owner);
//        // 验证流程相关数据
//        // 当前任务节点key
//        List<String> distinctOldList = oldTaskOwnerList.stream().distinct().collect(Collectors.toList());
//        String taskDefinitionKey = task.getTaskDefinitionKey();
//        if(CollectionUtils.isNotEmpty(nodes) && taskDefinitionKey.equals(DynProcessEnum.getCode(nodes.get(nodes.size() - 1).getTaskName()))){
//            issNodeRecord.setTaskName(nodes.get(nodes.size() - 1).getTaskName());
//            issNodeRecord.setNextTaskName(nodes.get(nodes.size() - 1).getTaskName());
//        }else{
//            // 暂时处理
//            Integer number = DynProcessEnum.getNumberByCode(taskDefinitionKey);
//            if(null != number){
//                issNodeRecord.setTaskName(DynProcessEnum.getName(taskDefinitionKey));
//                issNodeRecord.setNextTaskName(DynProcessEnum.getValueByNumber(number + 1));
//            }else{
//                issNodeRecord.setTaskName(DynProcessEnum.INITIATOR.name());
//                issNodeRecord.setNextTaskName(DynProcessEnum.SHORT_MEASURE.name());
//            }
//        }
//        issNodeRecord.setRemark(StringUtils.join(distinctOldList, ",") + "===>" +owner);
//        // 新增流程节点信息记录
//        this.insertNodeRecord(issNodeRecord, currentUser);
//    }
//
//}
